package com.xinlong.controller;

import com.github.pagehelper.PageInfo;
import com.xinlong.entity.OrderParam;
import com.xinlong.entity.Orders;
import com.xinlong.entity.product;
import com.xinlong.service.Ordersservice;
import com.xinlong.service.productservice;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.Map;

import java.util.List;

@Controller
@RequestMapping("/orders")
public class Orderscontroller {
    
     public static void main(String[]args){

    }



    @Autowired
    private Ordersservice  ordersservice;

    @Autowired
    private productservice productservice;

    /**
     * 查询全部订单信息
     * @param page
     * @param size
     * @param orderNum
     * @param orderStatus
     * @return
     * @throws Exception
     */
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "5") Integer size,
                                @RequestParam(defaultValue = "") String orderNum,@RequestParam(defaultValue = "0") String orderStatus) throws Exception {
        ModelAndView md=new ModelAndView();
        List<Orders> all = ordersservice.findAll(page,size,orderNum,orderStatus);
        PageInfo pageInfo=new PageInfo(all);
        System.out.println(pageInfo.getList());
        md.addObject("ordersList",pageInfo);
        md.setViewName("orders-list");
        return md;
    }

    @RequestMapping("/toAdd.do")
    public ModelAndView toAdd() throws Exception {
        ModelAndView md=new ModelAndView("orders-add");
        List<product> products = productservice.findByproductStatus(1);
        md.addObject("products",products);
        return md;
    }


    @RequestMapping("/add.do")
    public ModelAndView add(OrderParam orderParam,String productId,String orderDesc,String routeId) throws Exception {
        ModelAndView md=new ModelAndView("redirect:/orders/findAll.do");
        ordersservice.saveOrder(orderParam.getTravellers(),orderParam.getMember(),productId,orderDesc,routeId);
        return md;
    }

    @RequestMapping("selNumber")
    @ResponseBody
    public Map<String,Object> selNumber(Integer productId){
        Map<String,Object> map =  productservice.selNumber(productId);
        return map;
    }

    /**
     * 查询订单的详细信息
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/findById.do")
    public ModelAndView findbyid(String id)throws Exception{
        ModelAndView md=new ModelAndView();
        Orders orders= ordersservice.findbyidAll(id);
        List<product> products = productservice.findByproductStatus(1);
        md.addObject("products",products);
        md.addObject("orders",orders);
        System.out.println(orders);
        md.setViewName("orders-show1");
        return md;
    }

    @RequestMapping("/update.do")
    public ModelAndView update(OrderParam orderParam,String productId,String orderDesc,String routeId,String orderNum){
        ModelAndView md=new ModelAndView("redirect:/orders/findAll.do");
        try {
            ordersservice.update(orderParam.getMember(),orderParam.getTravellers(),productId,orderDesc,routeId,orderNum);
        } catch (Exception e) {
            md.setViewName("redirect:/failer.jsp");
            e.printStackTrace();
        }
        return md;
    }


}
